package main;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

public class SkillsParser {
	private final Plugin plugin;
	private File file;
	private FileConfiguration config;
	
	public SkillsParser(Plugin plugin) {
		this.plugin = plugin;
		file = new File(plugin.getDataFolder(), "skills.yml");
		try {
			firstRun(file);
		}
		catch (Exception e) {
			
		}		
		config = new YamlConfiguration();
		try {
			config.load(file);
		}
		catch (Exception e) {
		}
		plugin.reloadConfig();
		for(String key : config.getKeys(false)) {
			Logger.getLogger("Minecraft").info(key);		
		}
	}
	
	private void firstRun(File file) throws Exception {
	    if(!file.exists()){
	        file.getParentFile().mkdirs();
	        copy(plugin.getResource(file.getName()), file);
	    }
	}
	
	private void copy(InputStream in, File file) {
	    try {
	        OutputStream out = new FileOutputStream(file);
	        byte[] buf = new byte[1024];
	        int len;
	        while((len=in.read(buf))>0){
	            out.write(buf,0,len);
	        }
	        out.close();
	        in.close();
	    } 
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
	public void saveYaml() {
	    try {
	        config.save(file);
	    } 
	    catch (IOException e) {
	        e.printStackTrace();
	    }
	}
	
	public void loadYaml() {
	    try {
	    	config.load(file);
	    } 
	    catch (Exception e) {
	        e.printStackTrace();
	    }
	}
}
